from django.db.models.signals import post_save
from .models import Menu,PermissionsModel
from django.dispatch import receiver
import logging
methods = {'POST': '新增', 'GET': '查询', 'PUT': '修改', 'DELETE': '删除'}

@receiver(post_save,sender=Menu)
def create_memus_premission(sender,instance,created,**kwargs):
    if created:
        if isinstance(instance,Menu):  #说明是功能模块菜单
            if not instance.parent:
                permission=PermissionsModel.objects.create(name=instance.name+'的权限',is_menu=True)
              #  permission.menu=instance
                permission.save()

            else:
                for method in methods.keys():
                    permission=PermissionsModel.objects.create(name=f'{methods.get(method)}',method=method,
                                                               is_menu=False,path=instance.url,desc=f'{instance.name}的{methods.get(method)}的方法')
                    permission.menu=instance
                    permission.save()
        else:
            logging.info('不是Menu类型')


